package 剑指offer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: tyy 剑指 Offer
 * 260. 只出现一次的数字 III
 * 给你一个字符串数组 words ，只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
    美式键盘 中：
    第一行由字符 "qwertyuiop" 组成。
    第二行由字符 "asdfghjkl" 组成。
    第三行由字符 "zxcvbnm" 组成。
    示例 1：
    输入：words = ["Hello","Alaska","Dad","Peace"]
    输出：["Alaska","Dad"]
 * @create: 2021-10-31 17:40
 * @description:
 **/
public class Solution15 {
    /**对各个单词的每个字母在哪一行做统计，若某一行的统计数等于单词的长度则该表示单词的所有字母在同一行
     * @param words
     * @return
     */
    public String[] findWords(String[] words) {
        String s1 = "qwertyuiopQWERTYUIOP";
        String s2 = "asdfghjklASDFGHJKL";
        String s3 = "zxcvbnmZXCVBNM";
        List<String> list = new ArrayList<>();
        for(String  word : words){
            int n1 = 0, n2 = 0 , n3 = 0 ,leng = word.length();
            for(int i = 0 ; i < leng ; i++){
                if(s1.contains(word.charAt(i)+"")) n1++;
                else if(s2.contains(word.charAt(i)+"")) n2++;
                else  n3++;
            }
            if(n1 == leng || n2 == leng || n3 == leng) list.add(word);
        }
        return list.toArray(new String[list.size()]);
    }
    public String[] findWords1(String[] words) {
        String one = "qwertyuiop";
        String two = "asdfghjkl";
        String three = "zxcvbnm";
        List<String> list = Arrays.asList("qwertyuiop", "asdfghjkl", "zxcvbnm");
        List<String> resultList = new ArrayList<>();
        String[] result = null;
        for (int i = 0; i < words.length; i++) {
           String word = words[i].toLowerCase();
            char[] chars = word.toCharArray();
            int charLength = chars.length;
            boolean flag = true;
            // 使用goto语句（goto语句也称为无条件转移语句）
            tyy:for (int j = 0; j < charLength; j++) {
                String s = null;
                for (int k = 0; k < list.size(); k++) {
                    if (list.get(k).contains(String.valueOf(chars[0]))) {
                        s = list.get(k);
                        break;// 找出首字母所在行
                    }
                }
                if (s != null && !s.contains(String.valueOf(chars[j])) || s == null) {
                    flag = false;
                    break tyy;
                }// 单词不在同一行则跳出
            }
            if (flag) {
                resultList.add(words[i]);
            }
            result = resultList.toArray(new String[resultList.size()]);
        }
        return result;
    }
    public static void main(String[] args) {
        String nums1[] = {"Hello","Alaska","Dad","Peace"};
        String nums[] = {"adsdf","sfd"};
        String[] integers = new Solution15().findWords(nums);

        System.out.println("integers = " + Arrays.toString(integers));
    }
}